/*
 * This file is part of Plop Messenger.
 *
 * Plop Messenger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  Plop Messenger is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Plop Messenger.  If not, see <http://www.gnu.org/licenses/>.
 */
package pm.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import pm.Constantes;

public class Msg {
	private static Map<String, ResourceBundle> BUNDLES = new HashMap<String, ResourceBundle>();

	public static void addBundle(String bundle) {
		try {
			if (bundle.startsWith("file:"))
				BUNDLES.put(bundle,
						new PropertyResourceBundle(new FileInputStream(bundle
								.substring("file:".length()))));
			else
				BUNDLES.put(bundle, ResourceBundle.getBundle(bundle));
		} catch (MissingResourceException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String get(String key) {
		for (ResourceBundle b : BUNDLES.values()) {
			if (b.containsKey(key)) {
				return b.getString(key);
			}
		}
		return "!" + key + "!";
	}

	public static String get(String key, Object... params) {
		return MessageFormat.format(get(key), params);
	}

	public static void removeBundle(String bundle) {
		try {
			BUNDLES.remove(bundle);
		} catch (MissingResourceException e) {
			e.printStackTrace();
		}
	}
}
